package com.doeis.uwork.dao;

import java.util.ArrayList;
import java.util.List;

import com.doeis.common.utils.StringUtils;
import com.doeis.uwork.common.Constant;
import com.doeis.uwork.entity.Template;
import com.google.inject.Singleton;
import com.voland.core.db.Dao;
import com.voland.core.db.DaoException;
import com.voland.core.db.Page;

@Singleton
public class TemplateDao extends Dao {
	
	
	public Template findTemplateByid(Object id) throws DaoException{
		try{
			return super.readById(Template.class, id);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}

	public Template findTemplateByid(Object id, String userid) throws DaoException{
		try{
			String sql = "select * from template where id=? and user_id=? ";
			return super.read(Template.class, sql, id,userid);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}

	public Page<Template> listTemplates(Page page, Template templateForm,boolean doAssociate) throws DaoException{
		try{
			List<Object> params = new ArrayList<Object>();
			StringBuffer sql = new StringBuffer("select * from template where 1=1");
			if(templateForm.getUser_id() != null){
				sql.append(" and user_id=?");
				params.add(templateForm.getUser_id());
			}
			if (templateForm.getName()!=null) {
				sql.append(" and name like ? ");
				params.add("%"+templateForm.getName()+"%");
			}
			
			if(templateForm.getIs_top()!=null){
				sql.append(" and is_top = ?");
				params.add(templateForm.getIs_top());
			}
			sql.append(" order by sort_num ");
			return super.querySlice(Template.class, sql.toString(), page,doAssociate,params.toArray());
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	/**
	 * 查询用户所有的定制
	 * @param user_id
	 * @return
	 * @throws DaoException
	 * @author wxy
	 * @date 2014年8月5日 下午9:06:52
	 */
	public List<Template> findAllTemplates(String user_id,boolean doAssociate) throws DaoException{
		try{
			StringBuffer sql = new StringBuffer( "select * from template where user_id=? ");
			sql.append(" order by sort_num  ");
			 return super.query(Template.class, sql.toString(), doAssociate, user_id);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}

	/**
	 * 查询共享的定制
	 * @param page
	 * @param templateForm
	 * @param doAssociate
	 * @return
	 * @throws DaoException
	 * @author wxy
	 * 2014年9月24日星期三中国标准时间下午5:03:18
	 */
	public Page<Template> listSharedTemplates(Page<Template> page,
			Template templateForm, boolean doAssociate) throws DaoException {
		try{
			List<Object> params = new ArrayList<Object>();
			StringBuffer sql = new StringBuffer("select t.* from template t LEFT JOIN share s on s.source_id=t.id where s.source_type="+Constant.SHARE_TYPE_TEMPLATE);
			if(templateForm.getUser_id() != null){
				sql.append(" and t.user_id=?");
				params.add(templateForm.getUser_id());
			}
			if (templateForm.getName()!=null) {
				sql.append(" and t.name like ? ");
				params.add("%"+templateForm.getName()+"%");
			}
			
			if(templateForm.getIs_top()!=null){
				sql.append(" and t.is_top = ?");
				params.add(templateForm.getIs_top());
			}
			sql.append(" order by t.sort_num ");
			return super.querySlice(Template.class, sql.toString(), page,doAssociate,params.toArray());
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	/**
	 * 
	 */
	public Template findTemplate(Template templateForm) throws DaoException{
		try{
			List<Object> params = new ArrayList<Object>();
			StringBuffer sql = new StringBuffer("select * from template where 1=1");
			if(templateForm.getUser_id() != null){
				sql.append(" and user_id=?");
				params.add(templateForm.getUser_id());
			}
			if (templateForm.getName()!=null) {
				sql.append(" and name like ? ");
				params.add("%"+templateForm.getName()+"%");
			}
			
			if(templateForm.getIs_top()!=null){
				sql.append(" and is_top = ?");
				params.add(templateForm.getIs_top());
			}
			
			if(templateForm.getType() != null){
				sql.append(" and type=?");
				params.add(templateForm.getType());
			}
			sql.append(" order by sort_num ");
			return super.read(Template.class, sql.toString(), params.toArray());
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	/**
	 * 根据条件查询模版
	 * 需要自己加条件
	 * @param form
	 * @return
	 * @throws DaoException
	 */
	public List<Template> queryByCondition(Template form,boolean doAssociate) throws DaoException{
		try{
			List<Object> params = new ArrayList<Object>();
			StringBuffer sql = new StringBuffer( "select * from template where 1 ");
			if (!StringUtils.isBlank(form.getUser_id())) {
				sql.append(" and user_id=?");
				params.add(form.getUser_id());
			}
			if (!StringUtils.isBlank(form.getFrom_share_id())) {
				sql.append(" and from_share_id=?");
				params.add(form.getFrom_share_id());
			}
			sql.append(" order by sort_num  ");
			 return super.query(Template.class, sql.toString(), false, params.toArray());
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
}